import { Reducer, Effect, Subscription } from 'umi';
import { getRemoteList, editRecord, addRecord, deleteRecord } from './service'
import { message } from 'antd';
import { SingleUserType } from './data.d';

// 只要在仓库里面到处类型，就可以在 umi中使用
export interface UserState {
  data: SingleUserType[],
  meta: {
    total: number;
    per_page: number;
    page: number;
  };
}

interface UserModelType {
  namespace: 'users',
  state: UserState,
  subscriptions: {
    setup: Subscription
  },
  effects: {
    getRemote: Effect,
    // edit: Effect,
    // add: Effect,
    delete: Effect
  },
  reducers: {
    getList: Reducer<UserState>;
  },
}

const UserModel: UserModelType = {
  namespace: 'users',

  // 初始值
  state: {
    data: [],
    meta: {
      total: 0,
      per_page: 10,
      page: 1,
    }
  },

  // 订阅
  // dispatch(action)  action 是一个对象 { type:'getList', payload: {} }
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname }) => {
        if (pathname === '/users') {
          // 使用 pro-table 可以直接请求接口，不需要调用dispatch
          dispatch({
            type: 'getRemote',
            payload: {
              page: 1,
              per_page: 10,
            },
          });
        }
      })
    }
  },

  // 副作用处理异步，全部要变为 *函数
  // 使用 yield put调用 reducers
  // 参数是 action, effects
  effects: {
    *getRemote({ payload: { page, per_page } }, { put, call }) {
      const data = yield call(getRemoteList, { page, per_page });
      if (data) {
        yield put({
          type: 'getList',
          payload: data
        })
      }
    },

    // !添加和编辑方法已经不在走store了，在页面里直接请求
    // *edit({ payload: { id, values } }, { call, put, select }) {
    //   const data = yield call(editRecord, { id, values })

    //   if (data) {
    //     message.success('Edit Successfully!.')
    //     const { page, per_page } = yield select(
    //       (state: any) => state.users.meta,
    //     );

    //     yield put({
    //       type: 'getRemote',
    //       payload: {
    //         page,
    //         per_page,
    //       },
    //     })
    //   } else {
    //     message.error('Edit Faild.')
    //   }
    // },

    // *add({ payload: { values } }, { call, put, select }) {
    //   const data = yield call(addRecord, { values })

    //   if (data) {
    //     message.success('Add Successfully!.')
    //     const { page, per_page } = yield select(
    //       (state: any) => state.users.meta,
    //     );

    //     yield put({
    //       type: 'getRemote',
    //       payload: {
    //         page,
    //         per_page,
    //       },
    //     })
    //   } else {
    //     message.error('Add Faild.')
    //   }
    // },

    *delete({ payload: { id } }, { call, put, select }) {
      const data = yield call(deleteRecord, { id })

      if (data) {
        message.success('Delete Successfully!.')
        const { page, per_page } = yield select(
          (state: any) => state.users.meta,
        );

        yield put({
          type: 'getRemote',
          payload: {
            page,
            per_page,
          },
        })
      } else {
        message.error('Delete Faild.')
      }
    }
  },

  // 定义了修改规则
  // 返回页面数据的最后一步，所以一定要return
  // action = { type:'', payload: {} }
  reducers: {
    getList(state, { payload }) {

      return payload
    }
  },
}

export default UserModel
